//
//  NetWorkModel.m
//  Fangmm
//
//  Created by ChaoZhou on 16/3/3.
//  Copyright © 2016年 com.fangmm. All rights reserved.
//

#import "NetWorkModel.h"
#import <AFNetworking.h>
#define BASE_URL() alpha.fangmm.com/api3/v3

NSString *baseStr()
{
    NSString *baseString = @"http://api.fangmm.com/v3";
//    NSString *baseString = @"http://alpha.fangmm.com/api3/v3";
    return baseString;
}

@implementation NetWorkModel

+(void)getRequestkWithUrlString:(NSString*)subUrlString withHttpHeader:(NSDictionary*)headerDic withParas:(NSDictionary*)Paradic withSuccessBlock:(void(^)(NSDictionary*))successblock withFailBlock:(void(^)(id))failBlock
{
    NSString *urlStr = [NSString stringWithFormat:@"%@%@",baseStr(),subUrlString];
    NSString *urlstring = [urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    NSURL *URL = [NSURL URLWithString:urlstring];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"application/json",nil];
    if (headerDic)
    {
        [manager.requestSerializer setValue:headerDic.allValues.firstObject forHTTPHeaderField:headerDic.allKeys.firstObject];
    }
    NSMutableDictionary *params = [[NSMutableDictionary alloc]initWithDictionary:Paradic];
    [params setObject:@"user_ios" forKey:@"app"];
    [manager GET:URL.absoluteString parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {
        ;
    } success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary*  _Nullable responseObject)
    {
        NSLog(@"get reponse Data:%@",responseObject);
        if ([[NSString stringWithFormat:@"%@", responseObject[@"code"]]isEqualToString:@"0"])
        {
            successblock(responseObject);
        }
        else
        {
            failBlock(responseObject);
        }
    }
         failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error)
    {
        failBlock(error);
    }];
}

+(void)postRequestkWithUrlString:(NSString*)subUrlString withHeader:(NSDictionary*)headerDic withParas:(NSDictionary*)paraDic withSuccessBlock:(void(^)(NSDictionary*))successblock withFailBlock:(void(^)(id))failBlock
{
    NSString *urlStr = [NSString stringWithFormat:@"%@%@",baseStr(),subUrlString];
    NSString *urlstring = [urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    NSURL *URL = [NSURL URLWithString:urlstring];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"application/json", nil];
    if (headerDic)
    {
        [manager.requestSerializer setValue:headerDic.allValues.firstObject forHTTPHeaderField:headerDic.allKeys.firstObject];
    }
    NSMutableDictionary *params = [[NSMutableDictionary alloc]initWithDictionary:paraDic];
    [params setObject:@"user_ios" forKey:@"app"];
    [manager POST:URL.absoluteString parameters:params progress:^(NSProgress * _Nonnull uploadProgress)
     {
         ;
     }
          success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject)
     {
         if ([[NSString stringWithFormat:@"%@", responseObject[@"code"]]isEqualToString:@"0"])
         {
             successblock(responseObject);
         }
         else
         {
//             NSString *errorStr = [NSString stringWithFormat:@"ERROR:(CODE:%@,MSG:%@)",responseObject[@"code"],responseObject[@"msg"]];
             failBlock(responseObject);
         }
     }
          failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error)
     {
         failBlock(error);
     }];
}

+(void)putRequestkWithUrlString:(NSString*)subUrlString withHeader:(NSDictionary*)headerDic withParas:(NSDictionary*)paraDic withSuccessBlock:(void(^)(NSDictionary*))successblock withFailBlock:(void(^)(id))failBlock
{
    NSString *urlStr = [NSString stringWithFormat:@"%@%@",baseStr(),subUrlString];
    NSString *urlstring = [urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    NSURL *URL = [NSURL URLWithString:urlstring];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"application/json", nil];
    if (headerDic)
    {
        [manager.requestSerializer setValue:headerDic.allValues.firstObject forHTTPHeaderField:headerDic.allKeys.firstObject];
    }
    NSMutableDictionary *params = [[NSMutableDictionary alloc]initWithDictionary:paraDic];
    [params setObject:@"user_ios" forKey:@"app"];
    [manager PUT:URL.absoluteString parameters:params success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject)
    {
        if ([[NSString stringWithFormat:@"%@", responseObject[@"code"]]isEqualToString:@"0"])
        {
            successblock(responseObject);
        }
        else
        {
//            NSString *errorStr = [NSString stringWithFormat:@"ERROR:(CODE:%@,MSG:%@)",responseObject[@"code"],responseObject[@"msg"]];
            failBlock(responseObject);
        }
    }
         failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error)
    {
        failBlock(error);
    }];
}

+(void)deleteRequestkWithUrlString:(NSString *)subUrlString withHeader:(NSDictionary *)headerDic withParas:(NSDictionary *)paraDic withSuccessBlock:(void (^)(NSDictionary *))successblock withFailBlock:(void (^)(id))failBlock {
    NSString *urlStr = [NSString stringWithFormat:@"%@%@",baseStr(),subUrlString];
    NSString *urlstring = [urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    NSURL *URL = [NSURL URLWithString:urlstring];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"application/json", nil];
    if (headerDic)
    {
        [manager.requestSerializer setValue:headerDic.allValues.firstObject forHTTPHeaderField:headerDic.allKeys.firstObject];
    }
    NSMutableDictionary *params = [[NSMutableDictionary alloc]initWithDictionary:paraDic];
    [params setObject:@"user_ios" forKey:@"app"];
    [manager DELETE:URL.absoluteString parameters:params success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject)
     {
         if ([[NSString stringWithFormat:@"%@", responseObject[@"code"]]isEqualToString:@"0"])
         {
             successblock(responseObject);
         }
         else
         {
             NSString *errorStr = [NSString stringWithFormat:@"ERROR:(CODE:%@,MSG:%@)",responseObject[@"code"],responseObject[@"msg"]];
             failBlock(errorStr);
         }
     }
         failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error)
     {
         failBlock(error);
     }];
}

+(void)upLoadFileWithData:(NSData*)imageData withName:(NSString*)name toURL:(NSString*)subUrlString withParas:(NSDictionary*)paraDic withHeader:(NSDictionary*)headerDic withSuccessBlock:(void(^)(NSDictionary*))successblock withFailBlock:(void(^)(id))failBlock
{
    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
    NSString *urlStr = [NSString stringWithFormat:@"%@%@",baseStr(),subUrlString];
    NSString *urlstring = [urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    NSURL *URL = [NSURL URLWithString:urlstring];
    NSMutableDictionary *params = [[NSMutableDictionary alloc]initWithDictionary:paraDic];
    [params setObject:@"user_ios" forKey:@"app"];
    NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:URL.absoluteString parameters:params constructingBodyWithBlock:^(id formData) {
        [formData appendPartWithFileData:imageData name:name fileName:@"headimg.jpg" mimeType:@"image/png"];
    } error:nil];
    if (headerDic)
    {
        [request setValue:headerDic.allValues.firstObject forHTTPHeaderField:headerDic.allKeys.firstObject];
    }
    NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithStreamedRequest:request progress:nil completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error)
    {
        if (error)
        {
            failBlock(error);
        }
        else
        {
                                                  
            if ([[NSString stringWithFormat:@"%@", responseObject[@"code"]]isEqualToString:@"0"])
            {
                successblock(responseObject);
            }
            else
            {
                NSString *errorStr = [NSString stringWithFormat:@"ERROR:(CODE:%@,MSG:%@)",responseObject[@"code"],responseObject[@"msg"]];
                failBlock(errorStr);
            }
    }
    }];
    [uploadTask resume];
}
/*
-(void)uploadMutableDataWithUrlString:(NSString*)urlString withParas:(id)paras WithDataArray:(NSArray *)dataArray withSuccessBlock:(void(^)(NSDictionary *))successBlock withFailBlock:(void(^)(id))failBlock
{
//    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
//    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"application/json",nil];
//    [manager POST:@"" parameters:@"" constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData)
//    {
//        [formData appendPartWithFileData:@"Data数据" name:@"参数名称" fileName:@"文件名称  1.png" mimeType:@"文件类型  image/jpeg"];
//    
//        [formData appendPartWithFileURL:<#(nonnull NSURL *)#> name:<#(nonnull NSString *)#> error:<#(NSError *__autoreleasing  _Nullable * _Nullable)#>];
//        [formData appendPartWithFileURL:<#(nonnull NSURL *)#> name:<#(nonnull NSString *)#> fileName:<#(nonnull NSString *)#> mimeType:<#(nonnull NSString *)#> error:<#(NSError *__autoreleasing  _Nullable * _Nullable)#>];
//        [for<#(nonnull NSString *)#>mData appendPartWithFormData:<#(nonnull NSData *)#> name:];
//        [formData appendPartWithHeaders:(nullable NSDictionary<NSString *,NSString *> *) body:<#(nonnull NSData *)#>];
//        [formData appendPartWithInputStream:<#(nullable NSInputStream *)#> name:<#(nonnull NSString *)#> fileName:<#(nonnull NSString *)#> length:<#(int64_t)#> mimeType:<#(nonnull NSString *)#>];
//    } progress:^(NSProgress * _Nonnull uploadProgress) {
//        <#code#>
//    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//        <#code#>
//    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//        <#code#>
//    }];
//    [manager POST:<#(nonnull NSString *)#> parameters:<#(nullable id)#> constructingBodyWithBlock:<#^(id<AFMultipartFormData>  _Nonnull formData)block#> progress:<#^(NSProgress * _Nonnull uploadProgress)uploadProgress#> success:<#^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject)success#> failure:<#^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error)failure#>]

    
    
}
*/
+(void)uploadMutableDataWithUrlString:(NSString*)urlString withParas:(id)paras withHeaderDic:(NSDictionary *)headerDic  withFileName:(NSString *)fileName withName:(NSString *)name WithDataArray:(NSArray *)dataArray withSuccessBlock:(void(^)(NSDictionary *))successBlock withFailBlock:(void(^)(id))failBlock
{
    NSString *urlStr = [NSString stringWithFormat:@"%@%@",baseStr(),urlString];
    NSMutableDictionary *params = [[NSMutableDictionary alloc]initWithDictionary:paras];
    [params setObject:@"user_ios" forKey:@"app"];
    NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:urlStr parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData)
    {
        for (NSData *data in dataArray)
        {
            NSString *fileNameString =[NSString stringWithFormat:@"%d.png",arc4random()];
            [formData appendPartWithFileData:data name:name fileName:fileNameString mimeType:@"image/png"];
        }
        
    } error:nil];
    
    if (headerDic)
    {
        [request setValue:headerDic.allValues.firstObject forHTTPHeaderField:headerDic.allKeys.firstObject];
    }
    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    NSURLSessionUploadTask *uploadTask;
    uploadTask = [manager
                  uploadTaskWithStreamedRequest:request
                  progress:^(NSProgress * _Nonnull uploadProgress)
                  {
                     
                  }
                  completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error)
                  {
                      if (error)
                      {
                          NSLog(@"Error: %@", error);
                          failBlock(error);
                      } else
                      {
                          NSLog(@"%@ %@", response, responseObject);
                          successBlock(responseObject);
                      }
                  }];
    
    [uploadTask resume];
}
@end
